package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.common.cache.GmallCache;
import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.model.list.SearchAttr;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.mapper.*;
import com.atguigu.gmall.product.service.ProductDetailsApiService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductDetailsApiServiceImpl implements ProductDetailsApiService {

    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;

    @Autowired
    private SkuInfoMapper skuInfoMapper;

    @Autowired
    private SkuImageMapper skuImageMapper;
    
    @Autowired
    private SpuSaleAttrMapper spuSaleAttrMapper;
    
    @Autowired
    private SkuSaleAttrValueMapper skuSaleAttrValueMapper;

    @Autowired
    private BaseAttrInfoMapper baseAttrInfoMapper;

    @Autowired
    private SkuAttrValueMapper skuAttrValueMapper;
    //查询三级分类视图
    @GmallCache(prefix = "getBaseCategoryView")
    public BaseCategoryView getBaseCategoryView(Long category3Id) {
        QueryWrapper<BaseCategoryView> wrapper = new QueryWrapper();
        wrapper.eq("id",category3Id);
        BaseCategoryView baseCategoryView = baseCategoryViewMapper.selectOne(wrapper);
        return baseCategoryView;
    }
    //获取sku基本信息
    @GmallCache(prefix = RedisConst.SKUKEY_PREFIX,suffix = RedisConst.SKUKEY_SUFFIX)
    public SkuInfo getSkuInfo(Long skuId) {
        //1.查询sku基本信息
        QueryWrapper<SkuInfo> skuInfoWrapper = new QueryWrapper();
        QueryWrapper<SkuImage> skuImageWrapper = new QueryWrapper<>();
        skuInfoWrapper.eq("id",skuId);
        skuImageWrapper.eq("sku_id",skuId);
        SkuInfo skuInfo = skuInfoMapper.selectOne(skuInfoWrapper);
        //2.查询sku_images
        List<SkuImage> skuImages = skuImageMapper.selectList(skuImageWrapper);
        if(skuInfo!=null)
        skuInfo.setSkuImageList(skuImages);


        return skuInfo;
    }

    //获取销售属性
    @GmallCache(prefix = "getSaleAttr")
    public List<SpuSaleAttr> getSaleAttr(Long spuId) {
        List<SpuSaleAttr> spuSaleAttrs = spuSaleAttrMapper.spuSaleAttrList(spuId);
        return spuSaleAttrs;
    }

    //获取所有属性与属性值
    @GmallCache(prefix = "getSpuSaleAttrAndValue")
    public List<SpuSaleAttr> getSpuSaleAttrAndValue(Long spuId,Long skuId) {
        List<SpuSaleAttr> spuSaleAttrAndValue = spuSaleAttrMapper.getSpuSaleAttrAndValue(spuId,skuId);
        return spuSaleAttrAndValue;

    }
    @GmallCache(prefix = "getSkuValueIdsMap")
    //点击其他销售属性值的组合，跳转到另外的sku页面
    public Map<String, String> getSkuValueIdsMap(Long spuId) {
        List<Map> saleAttrValuesBySpu = skuSaleAttrValueMapper.getSaleAttrValuesBySpu(spuId);
        Map listMap = new HashMap<>();
        saleAttrValuesBySpu.forEach((map)->{
            listMap.put(map.get("value_ids"),map.get("sku_id"));
        });
        return listMap;
    }

    //获取价格
    public BigDecimal getPrice(Long skuId) {
        SkuInfo skuInfo = skuInfoMapper.selectById(skuId);
        return skuInfo.getPrice();
    }

    //获取所有一二三级分类
    public List<BaseCategoryView> getBaseCategoryList() {
        return baseCategoryViewMapper.selectList(null);
    }


    public List<SearchAttr> getSearchAttrList(Long skuId) {

        return baseAttrInfoMapper.getSearchAttrList(skuId);
    }

    public List<SkuAttrValue> getBaseAttrInfoAndBaseAttrValue(Long skuId){
        System.out.println(skuAttrValueMapper.getBaseAttrInfoAndBaseAttrValue(skuId));
        return skuAttrValueMapper.getBaseAttrInfoAndBaseAttrValue(skuId);
    }

}
